// pages/message/mContent/mContent.js
var app = getApp();
Page({

  
  data: {
    mcList:[],
    userInfo: {},
    flag:1,        //评论按钮的状态 1：点赞前   0：点赞后
    color:1,      //底部栏点赞按钮状态
    heart:1,       //底部栏喜欢按钮状态
    star:1,        //收藏按钮
    flag01:1,   //举报按钮
    comment:1,   //评论
    
  },


  // -----------------------页面数据加载模块 start------------------
  onLoad:function(options){
    var mid = options.mid;
    var userid = options.userid;
    //console.log("mid:"+mid);
    this.setData({
      mid:mid,
      userid:userid,
    });
    this.getSingleMessageByMid();  // 加载考研资讯/考研热点
    this.getUserInfo();  // 获取发表该咨询的用户信息
    this.getMessageComment(); // 获取资讯评论信息
  },

  onReachBottom: function() {
    this.getMessageComment();  // 下拉重新获取用户评论
  },


  // -----------------------页面数据加载模块 end------------------


  // -----------------------功能模块 start------------------

  // 获取考研资讯数据
  getSingleMessageByMid:function(){
    var mid = this.data.mid;
    var mark = 'getSingleMessage'
    wx.request({
      url: 'http://localhost:8080/youYanZaiXian/MessageServlet',
      method: 'POST',
      data:{
        mid:mid,
        mark:mark,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      success: (res)=>{
        var message = res.data.data.message;
        this.setData({
          message:message,
        })
        //console.log(this.data.message)
        
      }
    });
  },

  // 获取发表该咨询的用户信息
  getUserInfo:function(){
    var userid = this.data.userid;
    var mark = 'getUserInfo'
    wx.request({
      url: 'http://localhost:8080/youYanZaiXian/MessageServlet',
      method: 'POST',
      data:{
        userid:userid,
        mark:mark,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      success: (res)=>{
        var user = res.data.data.user;
        this.setData({
          user:user,
        })
        //console.log(this.data.user)
        
      }
    });
    
  },
  // 获取资讯评论信息
  getMessageComment:function(){
    var mid = this.data.mid;
    var mark = 'getMessageComment'
    wx.request({
      url: 'http://localhost:8080/youYanZaiXian/MessageServlet',
      method: 'POST',
      data:{
        mid:mid,
        mark:mark,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      success: (res)=>{
        var mcList = res.data.data.mcList;
        this.setData({
          mcList:mcList,
        })
        //console.log(this.data.mcList)
      }
    });
  },


  inputMcComment:function(e){
    var mcComment = e.detail.value;
    var mid = e.currentTarget.dataset.mid
    console.log("mcComment:"+mcComment);
    //console.log("mid:"+mid);
    this.setData({
      mcComment:mcComment,
      mid:mid,
    })
    this.sendMcComment();
  },

  //点击发送按钮收起评论框
  send:function(){
    this.setData({
      discussShow:false,
      comment:0,
      commentNum:Number(this.data.commentNum+1)
    })
  },

  sendMcComment:function(){
    var mark = 'sendMcComment';
    var mcComment = this.data.mcComment;
    //console.log(mcComment)
    var mid = this.data.mid;
    var userid =  getApp().globalData.userid;
    wx.request({
      url: 'http://localhost:8080/youYanZaiXian/MessageServlet',
      method: 'POST',
      data:{
        mark:mark,
        mcComment:mcComment,
        mid:mid,
        userid:userid,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
        'Accept': 'application/json'
      },
      success: (res)=>{
        var suc = res.data.data.suc;
        if(suc==1){
          wx.showToast({
            title: '评论成功',
            icon: 'success',
            duration: 2000
          })
        }else{
          wx.showToast({
            title: '评论失败',
            icon: 'error',
            duration: 2000
          })
        }
      }
    });
  },

   // -----------------------功能模块 end------------------



  //评论点赞
  reply:function(){
    if(this.data.flag){
      this.setData({
        flag:0,
        zanNum01:Number(this.data.zanNum01+1)
      })
    }else{
      this.setData({
        flag:1,
        zanNum01:Number(this.data.zanNum01-1)
      })
    }
    
  },

  //底部栏点赞
  goodNumChange:function(){
    if(this.data.color){
      this.setData({
        color:0,
        goodNum:Number(this.data.goodNum+1)
      })
    }else{
      this.setData({
        color:1,
        goodNum:Number(this.data.goodNum-1)
      })
    }
    
  },

  //底部栏喜欢按钮
  heartNumChange:function(){
    if(this.data.heart){
      this.setData({
        heart:0,
        heartNum:Number(this.data.heartNum+1)
      })
    }else{
      this.setData({
        heart:1,
        heartNum:Number(this.data.heartNum-1)
      })
    }
  },

  //底部栏收藏按钮
  starNumChange:function(){
    if(this.data.star){
      this.setData({
        star:0,
        starNum:Number(this.data.starNum+1)
      })
    }else{
      this.setData({
        star:1,
        starNum:Number(this.data.starNum-1)
      })
    }
  },

  //举报
  dislikeNumChange:function(){
    if(this.data.flag01){
      this.setData({
        flag01:0,
        // starNum:Number(this.data.starNum+1)
      })
    }else{
      this.setData({
        flag01:1,
        // starNum:Number(this.data.starNum-1)
      })
    }
  },

  //点击评论图标弹出评论框
  discussAction:function(e){
    // console.log('点击评论')
    // wx.showToast({
    //   title: '评论成功',
    //   icon:'none'
    // })
    this.setData({
      discussShow:true
    })
  },

})